package com.nsgf.library.ui.page.home;

import static android.view.View.GONE;
import static android.view.View.VISIBLE;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.video.impl.VideoCaptureConfig;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.RecyclerView;

import com.kunminx.architecture.ui.page.DataBindingConfig;
import com.kunminx.architecture.ui.state.State;
import com.kunminx.binding_recyclerview.adapter.BaseDataBindingAdapter;
import com.kunminx.binding_recyclerview.adapter.SimpleDataBindingAdapter;
import com.nsgf.library.data.bean.LibraryInfo;
import com.nsgf.library.data.bean.UserDataBean;
import com.nsgf.library.data.bean.UserInfo;
import com.nsgf.library.data.bean.XBannerBeanInfo;
import com.nsgf.library.data.response.ResponseStatus;
import com.nsgf.library.domain.request.AccountRequester;
import com.nsgf.library.glide.GlideImgManager;
import com.nsgf.library.ui.LiveBroadcastTestActivity;
import com.nsgf.library.ui.VideoConfigurationActivity;
import com.nsgf.library.ui.adapter.DiffUtils;
import com.nsgf.library.ui.mine.AddressListActivity;
import com.nsgf.library.ui.mine.InviterListActivity;
import com.nsgf.library.ui.mine.MineConsumptionPointsActivity;
import com.nsgf.library.ui.mine.MinePointsActivity;
import com.nsgf.library.ui.mine.MineRedPointsActivity;
import com.nsgf.library.ui.mine.SignInActivity;
import com.nsgf.library.ui.page.BaseFragment;
import com.nsgf.library.ui.page.DirectSaleStoreListActivity;
import com.nsgf.library.ui.page.LoginActivity;
import com.nsgf.library.ui.page.StateHolder;
import com.nsgf.library.ui.product.CouponListActivity;
import com.nsgf.library.utils.BaseAlterDialog;
import com.nsgf.library.utils.StringUtil;
import com.nsgf.library.utils.ToastUtils;
import com.stx.xhb.xbanner.XBanner;
import com.zhht.htzx.BR;
import com.zhht.htzx.R;
import com.zhht.htzx.databinding.FragmentNsgfMineBinding;
import com.zhht.htzx.databinding.ItemHomeMineBinding;
import com.zhht.htzx.ui.BusinessNewSettledActivity;
import com.zhht.htzx.ui.LoginTypeActivity;
import com.zhht.htzx.ui.OrderListActivity;
import com.zhht.htzx.ui.SettleActivity;
import com.zhht.htzx.ui.SharePosterActivity;
import com.zhht.htzx.ui.SharePosterDialogActivity;

import java.util.ArrayList;
import java.util.List;

public class MineFragment extends BaseFragment {

    private MineFragmentStates mStates;
    private AccountRequester mAccountRequester;

    @Override
    protected void initViewModel() {
        mStates = getFragmentScopeViewModel(MineFragmentStates.class);
        mAccountRequester = new AccountRequester();
    }

    private FragmentNsgfMineBinding mBinding;
    private SimpleDataBindingAdapter<LibraryInfo, ItemHomeMineBinding> mAdapter;
    private List<LibraryInfo> mDatas = new ArrayList<>();

    protected DataBindingConfig getDataBindingConfig() {

        mAdapter = new SimpleDataBindingAdapter<LibraryInfo, ItemHomeMineBinding>(getActivity(), R.layout.item_home_mine, DiffUtils.getInstance().getLibraryInfoItemCallback()) {
            @Override
            protected void onBindItem(ItemHomeMineBinding binding, LibraryInfo item, RecyclerView.ViewHolder holder) {

                binding.setVm(item);
            }
        };
        return new DataBindingConfig(R.layout.fragment_nsgf_mine, BR.vm, mStates)
                .addBindingParam(BR.adapter, mAdapter)
                .addBindingParam(BR.click, new ClickProxy());
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getLifecycle().addObserver(mAccountRequester);
        mBinding = (FragmentNsgfMineBinding) getBinding();


        mAdapter.setOnItemClickListener(new BaseDataBindingAdapter.OnItemClickListener<LibraryInfo>() {
            @Override
            public void onItemClick(int viewId, LibraryInfo item, int position) {
                if (5 == position) {
                    UserInfo.getInstance().clearUserInfo();
                    onResume();
                    startActivity(new Intent().setClass(getActivity(), LoginTypeActivity.class));
                    return;
                } else if (position == 10) {
                    startActivity(new Intent().setClass(getActivity(), DirectSaleStoreListActivity.class));
                    return;
                }
                if (StringUtil.isEmpty(UserInfo.getInstance().getTokenValue())) {
                    startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
                    return;
                }
                if (position == 0) {
                    Intent intent = new Intent(getActivity(), SharePosterActivity.class);
                    startActivity(intent);
                } else if (position == 4) {
                    ToastUtils.showShortToast("敬请期待!");
                } else if (position == 6) {

                    new BaseAlterDialog(getActivity(), R.style.dialogstyle, "确定要退出当前登录用户吗？",
                            "确定",
                            "取消",
                            (dialog, confirm) -> {
                                UserInfo.getInstance().clearUserInfo();
                                onResume();
                                dialog.dismiss();
                            }, new BaseAlterDialog.OnCloseListener() {
                        @Override
                        public void onCancleClick(Dialog dialog, boolean confirm) {
                            dialog.dismiss();
                        }
                    }).show();
                } else if (position == 7)
                    startActivity(new Intent().setClass(getActivity(), SignInActivity.class));
                else if (position == 3) {
                    startActivity(new Intent().setClass(getActivity(), AddressListActivity.class));
                } else if (position == 1) {
                    startActivity(new Intent().setClass(getActivity(), InviterListActivity.class));
                } else if (position == 2) {
                    startActivity(new Intent().setClass(getActivity(), CouponListActivity.class));
                } else if (position == 8) {
                    mBinding.getClick().gotoPoints(8);
                } else if (position == 9) {
                    startActivity(new Intent().setClass(getActivity(), BusinessNewSettledActivity.class));
                }else if (position == 11) {
                    startActivity(new Intent().setClass(getActivity(), LiveBroadcastTestActivity.class));
                }else if (position == 12) {
                    startActivity(new Intent().setClass(getActivity(), VideoConfigurationActivity.class));
                }

            }
        });

        mDatas.add(new LibraryInfo("邀请有礼", "", "", R.drawable.ic_home_mine_invitation_with_gifts));
        mDatas.add(new LibraryInfo("我的邀请", "", "", R.drawable.ic_home_mine_my_invitation));
        mDatas.add(new LibraryInfo("新人福利", "", "", R.drawable.ic_home_mine_benefit));
        mDatas.add(new LibraryInfo("收货地址", "", "", R.drawable.ic_home_mine_address));
        mDatas.add(new LibraryInfo("帮助中心", "", "", R.drawable.ic_home_mine_que));
        mDatas.add(new LibraryInfo("切换", "", "", R.drawable.ic_home_mine_lout_user));
        mDatas.add(new LibraryInfo("退出登录", "", "", R.drawable.ic_home_mine_logout));
        mDatas.add(new LibraryInfo("每日签到", "", "", R.drawable.ic_home_mine_sign));
        mDatas.add(new LibraryInfo("爱心基金", "", "", R.drawable.ic_home_mine_love));
        mDatas.add(new LibraryInfo("商家入驻", "", "", R.drawable.ic_home_mine_business));
        mDatas.add(new LibraryInfo("直营店", "", "", R.drawable.ic_home_mine_business_type));
        mDatas.add(new LibraryInfo("看直播", "", "", R.drawable.ic_home_mine_business_type));
        mDatas.add(new LibraryInfo("开直播", "", "", R.drawable.ic_home_mine_business_type));

        mStates.list.set(mDatas);
        mAdapter.notifyDataSetChanged();


        mAccountRequester.getUserResultProject().observe(this, new Observer<ResponseStatus<UserDataBean>>() {
            @Override
            public void onChanged(ResponseStatus<UserDataBean> userDataBeanResponseStatus) {

                if (0 == userDataBeanResponseStatus.getResponseCode()) {
                    UserInfo.getInstance().setName(userDataBeanResponseStatus.getData().getNickname());
                    UserInfo.getInstance().setAppLogo(userDataBeanResponseStatus.getData().getAvatar());
                    mStates.avatar.set(userDataBeanResponseStatus.getData().getAvatar());
                    mBinding.llMineUserInfo.setVisibility(VISIBLE);
                    mBinding.tvLogin.setVisibility(GONE);
                    mStates.userDataBeanState.set(userDataBeanResponseStatus.getData());
                    banners.clear();
                    for (int i = 0; i < userDataBeanResponseStatus.getData().getCarouselList().size(); i++) {
                        XBannerBeanInfo info = new XBannerBeanInfo(userDataBeanResponseStatus.getData().getCarouselList().get(i));
                        banners.add(info);
                    }
                    mBinding.xbanner.setBannerData(banners);
                    mBinding.xbanner.loadImage((banner, model, bannerView, position) -> {
                        String url = (String) ((XBannerBeanInfo) model).getXBannerUrl();
                        GlideImgManager.getInstance().glideLoaderBannerMineCenterCrop(getActivity(),
                                url.replace("\\", "/"), (ImageView) bannerView, 10);

                    });
                    mBinding.xbanner.setOnItemClickListener(new XBanner.OnItemClickListener() {
                        @Override
                        public void onItemClick(XBanner banner, Object model, View view, int position) {
                            startActivity(new Intent().setClass(getActivity(), SharePosterDialogActivity.class));
                        }
                    });
                } else {
                    mBinding.llMineUserInfo.setVisibility(GONE);
                    mBinding.tvLogin.setVisibility(VISIBLE);
                }

            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        if (1 != UserInfo.getInstance().getIsMerchant()) {
            if (!StringUtil.isEmpty(UserInfo.getInstance().getTokenValue())) {
                mBinding.llMineUserInfo.setVisibility(VISIBLE);
                mBinding.tvLogin.setVisibility(GONE);
                mBinding.xbanner.setVisibility(VISIBLE);
                mAccountRequester.getNewLoginInfo();
            } else {
                mStates.avatar.set("");
                mBinding.llMineUserInfo.setVisibility(GONE);
                mBinding.tvLogin.setVisibility(VISIBLE);
                mBinding.xbanner.setVisibility(GONE);
            }
        }
    }

    private List<XBannerBeanInfo> banners = new ArrayList<>();

    public static class MineFragmentStates extends StateHolder {

        public final State<String> name = new State<>(UserInfo.getInstance().getName());
        public final State<Boolean> loadingVisible = new State<>(false);
        public final State<List<LibraryInfo>> list = new State<>(new ArrayList<>());

        public final State<UserDataBean> userDataBeanState = new State<>(new UserDataBean());


    }

    public class ClickProxy {

        private String titlebar = "";

        public void openSettle() {
            startActivity(new Intent().setClass(getActivity(), SettleActivity.class));
        }

        public void gotoPoints(int index) {

            if (StringUtil.isEmpty(UserInfo.getInstance().getTokenValue())) {
                ToastUtils.showShortToast("请先登录");
                loginGoto();
                return;
            }
            if (0 == index) {
                titlebar = "福利积分";
            } else if (1 == index) {
                titlebar = "消费积分";
                startActivity(new Intent()
                        .putExtra("title", titlebar)
                        .putExtra("index", index)
                        .putExtra("data", mStates.userDataBeanState.get())
                        .setClass(getActivity(), MineConsumptionPointsActivity.class));
                return;
            } else if (2 == index) {
                titlebar = "分红点";
                startActivity(new Intent()
                        .putExtra("title", titlebar)
                        .putExtra("index", index)
                        .putExtra("data", mStates.userDataBeanState.get())
                        .setClass(getActivity(), MineRedPointsActivity.class));
                return;
            } else if (3 == index) {
                titlebar = "贡献值";
                startActivity(new Intent()
                        .putExtra("title", titlebar)
                        .putExtra("index", index)
                        .putExtra("data", mStates.userDataBeanState.get())
                        .setClass(getActivity(), MineRedPointsActivity.class));
                return;
            } else if (4 == index) {
                titlebar = "惠通宝";
                startActivity(new Intent()
                        .putExtra("title", titlebar)
                        .putExtra("index", index)
                        .putExtra("data", mStates.userDataBeanState.get())
                        .setClass(getActivity(), MineConsumptionPointsActivity.class));
                return;
            } else if (index == 8) {
                titlebar = "爱心基金";
                startActivity(new Intent()
                        .putExtra("title", titlebar)
                        .putExtra("index", index)
                        .putExtra("data", mStates.userDataBeanState.get())
                        .setClass(getActivity(), MineRedPointsActivity.class));
                return;

            } else titlebar = "";
            startActivity(new Intent()
                    .putExtra("title", titlebar)
                    .putExtra("index", index)
                    .putExtra("data", mStates.userDataBeanState.get())
                    .setClass(getActivity(), MinePointsActivity.class));
        }

        public void loginGoto() {
            startActivity(new Intent().setClass(getActivity(), LoginActivity.class));
        }

        public void ActivityGotoOrder(int index) {
            if (StringUtil.isEmpty(UserInfo.getInstance().getTokenValue())) {
                ToastUtils.showShortToast("请先登录");
                loginGoto();
                return;
            }
            startActivity(new Intent()
                    .putExtra("index", index)
                    .setClass(getActivity(), OrderListActivity.class));
        }

    }
}
